<template>
  <div
    class="dynamic"
    @click="toDetail(item.id)"
    v-for="item in dataList"
    :key="item.id"
  >
    <var-image
      width="45px"
      height="45px"
      fit="cover"
      :radius="5"
      :src="item.avatar"
    />
    <div class="right">
      <div class="name">
        <span>{{ item.username }}</span>
        <var-chip v-show="item.status == 0" plain size="mini" type="primary"
          >审核中</var-chip
        >
        <var-chip v-show="item.status == 2" plain size="mini" type="danger"
          >被驳回</var-chip
        >
      </div>

      <var-ellipsis
        :tooltip="false"
        v-if="item.content"
        class="content"
        :line-clamp="5"
        >{{ item.content }}</var-ellipsis
      >
      <div v-if="item.community_img.length" class="img-container">
        <var-image
          v-for="img in item.community_img"
          :key="img"
          @click.stop="preview(img)"
          fit="cover"
          :src="img"
        />
      </div>
      <span class="footer">
        <span>{{ dayjs(item.createdAt).fromNow() }}</span>
        <var-link
          v-show="item.userId === infoStore.userInfo.id"
          @click.stop="handleDelete(item.id)"
          type="danger"
          underline="none"
          >删除</var-link
        >
      </span>
    </div>
  </div>
</template>

<script setup lang="ts">
import dayjs from '@/utils/dayjs'
import { deleteDynamic } from '@/api'
import { useInfoStore } from '@/store/info'

const props = defineProps<{
  dataList: any[]
}>()

const emit = defineEmits(['delete'])

const router = useRouter()
const infoStore = useInfoStore()

const preview = img => {
  ImagePreview(img)
}

const toDetail = id => {
  router.push(`/dynamic-detail/${id}`)
}

const handleDelete = async id => {
  const res = await Dialog('删除动态后无法恢复,是否删除?')
  if (res !== 'confirm') return
  const msg = await deleteDynamic(id)
  Snackbar.success(msg)
  emit('delete', id)
}
</script>

<style scoped lang="scss">
.dynamic {
  display: flex;
  padding: 5px;
  margin: 10px 0;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
  border-radius: 5px;

  .right {
    flex: 1;
    padding: 0 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .name {
      font-size: 16px;
      font-weight: 500;
      margin-bottom: 5px;

      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .content,
    .img-container {
      margin: 8px 0;
    }

    .img-container {
      display: flex;
      justify-content: space-between;
      .var-image {
        flex: 1;
        margin-right: 5px;
      }
    }
    .content {
      font-size: 14px;
      color: #666;
    }

    .footer {
      font-size: 12px;
      color: #999;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
}
</style>
